/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package gui.tools.models;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import lib.plan.Order;

/**
 * This class is used as a model to show orders
 * It shows the most relevant properties of the order as the number, the priority,
 * the date when it was recived and the date when it must be done
 * It allows to add and remove orders from the model, updating the view of the table
 * @see Order
 * @see JTable
 * @see TableModel
 * @see DefaultTableModel
 * @author Jorge Mira Yagüe
 */
public class TMOrder extends DefaultTableModel {

    private List<Order> orders;

    /**
     * Class builder
     * @param orders The order list to show in the table
     */
    public TMOrder(List<Order> orders) {
        super();

        columnIdentifiers.add("Number");
        columnIdentifiers.add("Customer");
        columnIdentifiers.add("Priority");
        columnIdentifiers.add("Date reicived");
        columnIdentifiers.add("Date due");
        setOrders(orders);
    }

    public List<Order> getOrders() {
        return orders;
    }

    public Order getOrder(int index) {
        return orders.get(index);
    }

    public void setOrder(int index, Order order) {
        orders.set(index, order);
        refresh();
    }

    public void addOrder(Order order) {
        orders.add(order);
        refresh();
    }

    public Order getDistrubution(int index) {
        return orders.get(index);
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
        refresh();
    }

    public void removeOrder(int index) {
        orders.remove(index);
        refresh();
    }

    private void refresh() {
        dataVector = new Vector();
        Vector<String> v;
        DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy-HH:mm");
        for (Order o : orders) {
            v = new Vector();
            v.add(String.valueOf(o.getNumber()));
            v.add(o.getCustomer());
            v.add(String.valueOf(o.getPriority()));
            v.add(formatter.format(o.getDateReceived().getTime()));
            v.add(formatter.format(o.getDateDue().getTime()));
            dataVector.add(v);
        }
    }

    /**
     * This function makes the cells not editables by the user
     * @param x
     * @param y
     * @return always false
     */
    @Override
    public boolean isCellEditable(int x, int y) {
        return false;
    }
}
